﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace UrPortCommunicate
{
    public enum DashCommand
    {
        load,
        getLoadedProgram,
        play,
        stop,
        pause,
        isProgramSaved,
        programState,
        shutdown,
        running,
        robotmode,
        popup,
        closepopup,
        addToLog,
        polyscopeVersion,
        powerOn,
        powerOff,
        brakeRelease,
        safetymode,
        unlockProtectiveStop,
        closeSafetyPopup,
        safetyStatus,
        restartSafety
    }
    public class UrClient29999
    {
        private readonly Mutex mutex = new Mutex();
        Socket clientSocket;
        public bool Open(string ip, int port)
        {
            bool result;
            if (clientSocket != null)
            {
                clientSocket.Close();
            }
            clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            //服务端链接ip与端口号
            clientSocket.Connect(new IPEndPoint(IPAddress.Parse(ip), port));
            byte[] rece = new byte[1024];
            clientSocket.Receive(rece);
            return true;
        }

        public bool Close()
        {
            clientSocket.Close();
            return true;
        }

        public string SendCommand(DashCommand cmd,string str)
        {

            try
            {
                mutex.WaitOne();
                string space = str == "" ? "" : " ";
                clientSocket.Send(Encoding.UTF8.GetBytes(command[cmd] + space + str + "\n"));
                clientSocket.ReceiveTimeout = 3000;
                byte[] rece = new byte[1024];
                int size=clientSocket.Receive(rece);
                rece=rece.Take<byte>(size).ToArray();
                mutex.ReleaseMutex();
                string recestr = System.Text.Encoding.Default.GetString(rece);
                return recestr;
            }
            catch (Exception e)
            {
                mutex.ReleaseMutex();
                 throw e;
            }

        }



        public Dictionary<DashCommand, String> command = new Dictionary<DashCommand, string>
        {
            {DashCommand.load,"load" },
            {DashCommand.getLoadedProgram,"get loaded program" },
            {DashCommand.play,"play" },
            {DashCommand.stop,"stop" },
            {DashCommand.pause,"pause" },
            {DashCommand.isProgramSaved,"isProgramSaved" },
            {DashCommand.programState,"programState" },
            {DashCommand.shutdown,"shutdown" },
            {DashCommand.running,"running" },
            {DashCommand.robotmode,"robotmode" },
            {DashCommand.popup,"popup" },
            {DashCommand.closepopup,"close popup" },
            {DashCommand.addToLog,"addToLog" },
            {DashCommand.polyscopeVersion,"polyscopeVersion" },
            {DashCommand.powerOn,"power on" },
            {DashCommand.powerOff,"power off" },
            {DashCommand.brakeRelease,"brake release" },
            {DashCommand.safetymode,"safetymode" },
            {DashCommand.unlockProtectiveStop,"unlock protective stop" },
            {DashCommand.closeSafetyPopup,"close safety popup" },
            {DashCommand.safetyStatus,"safetystatus" },
            {DashCommand.restartSafety,"restart safety" }
        };
    }
}
